package sword.T34;

import util.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: duyuzhou
 * @Create: 2022-04-07 22:43:06 星期四
 */

class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int target) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        if (root != null)
            dfs(root, result, path, 0, target);
        return result;
    }

    private void dfs(TreeNode root, List<List<Integer>> result, List<Integer> path, int cur, int target) {
        if (root.left == null && root.right == null) {
            if (cur+root.val == target) {
                path.add(root.val);
                result.add(new ArrayList<>(path));
                path.remove(path.size()-1);
            }
            return;
        }
        path.add(root.val);
        if (root.left != null)
            dfs(root.left, result, path, cur+root.val, target);
        if (root.right != null)
            dfs(root.right, result, path, cur+root.val, target);
        path.remove(path.size()-1);
    }
}